package shining365.programmableweb.data.app;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.xml.bind.JAXBException;

import org.junit.Test;

import shining365.programmableweb.data.model.Api;

public class GenerateRGraphs extends AppBasic {
	
	public GenerateRGraphs() throws IOException, JAXBException {
		super(false, OrderBy.USE_COUNT);
	}
	
	@Test
	public void getAllApis() throws FileNotFoundException {
		PrintWriter out = new PrintWriter("var/allApis.r.txt");
		
		out.printf("src\tdest\tweight\n");
		for (int i = 0; i < nNodes - 1; i++) {
			for (int j = i + 1; j < nNodes; j++) {
				if (graph[i][j] != 0) {
					out.printf("%s %s %f\n", apis.get(i).getUrl(), 
							apis.get(j).getUrl(), graph[i][j]);
				}
			}
		}
		
		out.close();
	}
	
	@Test
	public void genTopApis() throws FileNotFoundException {
		// 点数
		final int MAX_APIS = config.containsKey("genTable.maxApis") ? config
				.getInt("genTable.maxApis") : 30;
		// 点的最大度
		final int MAX_RELATED = config.containsKey("genTable.maxRelated") ? config
				.getInt("genTable.maxRelated")
				: MAX_APIS;
		// 边的最小权值
		final int MIN_WEIGHT = config.containsKey("genTable.minWeight") ? config
				.getInt("genTable.minWeight")
				: 3;
		
		PrintWriter out = new PrintWriter("var/topApis.r.txt");
		
		List<Api> topApis = new ArrayList<Api>();
		for (int i = 0; i < MAX_APIS; i++) {
			topApis.add(apis.get(i));
		}
		
		out.println("src\tdest\tweight");
		for (int i = 0; i < MAX_APIS; i++) {
			final int id = i;
			List<Api> aRelated = new ArrayList<Api>(topApis);
			Collections.sort(aRelated, new Comparator<Api>() {
				@Override
				public int compare(Api o1, Api o2) {
					return new Double(graph[id][indexOf(o2)])
							.compareTo(graph[id][indexOf(o1)]);
				}
			});
			int nRelated = MAX_RELATED;
			for (Api b : aRelated) {
				if (apis.get(i) == b) {
				//不要自环
					continue;
				}
				if (nRelated-- <= 0) {
				//已到达设定的边数
					break;
				}
				if (graph[i][indexOf(b)] >= MIN_WEIGHT) {
					out.printf("%s\t%s\t%s\n", apis.get(i).getUrl(), 
							apis.get(indexOf(b)).getUrl(), 
							graph[i][indexOf(b)]);
				}
			}
		}
		out.close();
	}
	
	@Test
	public void nodeUseCount() throws FileNotFoundException {
		PrintWriter out = new PrintWriter("var/nodeUseCount.r.txt");
		
		out.printf("node weight\n");
		for (int i = 0; i < nNodes; i++) {
			out.printf("%s %d\n", apis.get(i).getUrl(), useCounts[i]);
		}
		
		out.close();
	}
	
	@Test
	public void nodeComputeStrength() throws FileNotFoundException {
		PrintWriter out = new PrintWriter("var/nodeComputeStrength.r.txt");
		
		out.printf("node weight\n");
		for (int i = 0; i < nNodes; i++) {
			double strength = graph[i][i];
			for (int j = 0; j < nNodes; j++) {
				strength += graph[i][j];
			}
			out.printf("%s %d\n", 
					apis.get(i).getUrl(), (int)Math.round(strength));
		}
		
		out.close();
	}

	@Test
	public void nodeStrength() throws FileNotFoundException {
		PrintWriter out = new PrintWriter("var/nodeStrength.r.txt");
		
		out.printf("node weight\n");
		for (int i = 0; i < nNodes; i++) {
			out.printf("%s %d\n", 
					apis.get(i).getUrl(), (int)Math.round(strengths[i]));
		}
		
		out.close();
	}
	
}
